<template>

  <a-spin :spinning="confirmLoading">
    <a-form :form="form">

      <a-row>

        <!-- 子表单区域 -->
        <CustomerInfo ref="customerInfo"></CustomerInfo>



        <a-col :span="24" style="margin-top: 15px">
          <a-form-item label="标题" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
            <a-input v-if="!disabled" v-decorator="['title', validatorRules.title]" rows="4" placeholder="请输入标题" />
            <span v-else>{{ data.title }}</span>
          </a-form-item>
        </a-col>

        <a-col :span="24">
          <a-form-item label="申请说明" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
            <a-textarea v-if="!disabled" v-decorator="['processDescription', validatorRules.processDescription]"
                        rows="4" placeholder="请输入申请说明" />
            <span v-else>{{ data.processDescription }}</span>
          </a-form-item>
        </a-col>

      </a-row>

    </a-form>

    <BaseForm v-if="!disabled" ref="baseForma"></BaseForm>

    <a-form-item v-if="!disabled"
                 :wrapperCol="{ span: 24 }"
                 style="text-align: center">
      <a-button type="primary" :disabled="disabled || btndisabled" @click="handleSubmit">保存</a-button>
      <a-button style="margin-left: 8px" :disabled="disabled" @click="close">取消</a-button>
    </a-form-item>

    <a-form-item v-if="task"
                 :wrapperCol="{ span: 24 }"
                 style="text-align: center">
      <a-button type="primary" @click="passTask">通过</a-button>
      <a-button style="margin-left: 8px" @click="backTask">驳回</a-button>
    </a-form-item>
  </a-spin>
</template>

<script>

import pick from 'lodash.pick'
import '@/assets/less/TableExpand.less'
import JDate from '@/components/jeecg/JDate'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
import { ActivitiFormMixin } from '@/views/activiti/mixins/activitiFormMixin'
import JEditableTable from '@comp/jeecg/JEditableTable'
import { AssetSubMixin } from '@api/assetSubMixin'
import BaseForm from './BaseForm'
import CustomerInfo from './CustomerInfo'
import { VALIDATE_NO_PASSED, validateFormAndTables } from '@/utils/JEditableTableUtil'
import { httpAction } from '@api/manage'

export default {
  name: 'CustomerBalanceRefund',
  mixins: [ActivitiFormMixin, AssetSubMixin],
  components: {
    BaseForm,
    CustomerInfo,
    JEditableTable,
    JDate,
    JDictSelectTag,
    JSearchSelectTag
  },
  data () {
    return {
      // form
      form: this.$form.createForm(this),
      refKeys: ['customerInfo', 'customerInfoOld'],
      tableKeys: ['customerInfo', 'customerInfoOld'],
      activeKey: 'customerInfo',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      validatorRules: {
        title: {
          rules: [
            { required: true, message: '请输入标题!' }
          ]
        },
        processDescription: {
          rules: [
            { required: true, message: '请输入申请说明!' }
          ]
        }
      }
    }
  },
  created () {
    this.init()
  },
  methods: {
    setValue () {
      // this.$refs.customerInfo.disableSubmit = this.disabled
      this.$refs.customerInfo.disableSubmit2 = this.disabled
      this.$refs.customerInfo.showModal(this.data.dataMap)
      this.data.title = this.processData.title
      if (!this.disabled) {
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.data, 'processDescription', 'title'))
          if (this.processData.procDefId) {
            let keys = []
            keys.push(this.processData.procDefId)
            this.$refs.baseForma.selectedRowKeys = keys
          }
          this.$refs.baseForma.queryParam = { status: 1, routeName: this.processData.routeName }
          this.$refs.baseForma.loadData(1)
        })
      }/* else {
        // this.$refs.customerInfoOld.disableSubmit = this.disabled
        this.$refs.customerInfoOld.setProcessData(this.processData, this.disabled)
        this.$refs.customerInfoOld.showModal(this.oldDataMap)
      }*/
    },
    // handler
    handleSubmit (e) {
      let that = this
      /** 触发表单验证 */
      this.$refs.customerInfo.form.validateFields((err, allValues) => {
        if (!err) {
          let a = Object.assign(this.$refs.customerInfo.model, allValues)
          this.form.validateFields((err, values) => {
            if (!err) {
              if (this.$refs.baseForma.selectedRowKeys.length != 1) {
                this.$message.warning('请选择流程图')
                return
              }

              this.confirmLoading = true
              a.customerId = a.id
              httpAction(this.$refs.customerInfo.url.balanceRefundCheck, a, 'POST').then((res) => {
                if (res.success) {
                  let formData = values
                  formData.data = a
                  formData.oldData = this.oldDataMap
                  formData.procDefId = this.$refs.baseForma.selectedRowKeys[0]
                  that.handleSubmitVa(formData)
                } else {
                  this.$message.warning(res.message)
                }
              }).finally(() => {
                this.confirmLoading = false
              })
            }
          })
        }
      })
    }
  }
}
</script>

<!--lang="less"-->
<style lang="less" scoped>
@import '~@assets/less/common.less';

.ant-form-item {
  margin-bottom: 10px;
}

.ant-col {
  min-height: 51px !important;
}

/*.ant-form-item span {*/
/*  position: absolute !important;*/
/*  min-height: 20px !important;*/
/*}*/
</style>